package org.jeecg.modules.crawler.job;

import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.wanghaomiao.xpath.exception.NoSuchAxisException;
import cn.wanghaomiao.xpath.exception.NoSuchFunctionException;
import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.activity.entity.ActivityInfo;
import org.jeecg.modules.activity.service.IActivityInfoService;
import org.jeecg.modules.content.entity.ContentInfo;
import org.jeecg.modules.content.service.IContentInfoService;
import org.jeecg.modules.crawler.controller.NewCrawlerController;
import org.jeecg.modules.crawler.entity.NewCrawlerInfo;
import org.jeecg.modules.crawler.entity.NewCrawlerKeyword;
import org.jeecg.modules.crawler.service.INewCrawlerInfoService;
import org.jeecg.modules.crawler.service.INewCrawlerKeywordService;
import org.jsoup.nodes.Element;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务定时发送
 */
@Slf4j
public class bak implements Job {

    @Autowired
    private INewCrawlerInfoService newCrawlerInfoService;

    @Autowired
    private INewCrawlerKeywordService newCrawlerKeywordService;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<NewCrawlerKeyword> keywordList = newCrawlerKeywordService.list();

        try {
            for (NewCrawlerKeyword newCrawlerKeyword : keywordList) {
                for (int i = 1; i < 6; i++){
                    String queryStr = "[{\"field\":\"pageIndex\",\"value\":"+i+"},{\"field\":\"group\",\"value\":0},{\"field\":\"searchType\",\"value\":\"\"},{\"field\":\"keyword\",\"value\":\"" + newCrawlerKeyword.getKeyword() + "\"},{\"field\":\"recommend\",\"value\":\"1\"},{\"field\":4,\"value\":\"\"},{\"field\":5,\"value\":\"\"},{\"field\":6,\"value\":\"\"},{\"field\":7,\"value\":\"\"}]";
                    String query = Base64Encoder.encode(queryStr.getBytes(StandardCharsets.UTF_8));
                    List<NewCrawlerInfo> crawlerInfos = new ArrayList<>();
                    Map<String, Object> fromMap = new HashMap<>();
                    fromMap.put("searchInfo", query);
                    HttpRequest post = HttpUtil.createPost("https://www.seu.edu.cn/_web/_search/api/searchCon/create.rst?_p=YXM9MiZ0PTI0OTImZD05NTk1JnA9MSZtPVNOJg__&tt=" + Math.random());
                    post.header("Referer", "https://www.seu.edu.cn/_web/_search/api/search/new.rst?locale=zh_CN&request_locale=zh_CN&_p=YXM9MiZ0PTI0OTImZD05NTk1JnA9MSZtPVNOJg__");
                    post.form(fromMap);
                    HttpResponse httpResponse = post.execute();
                    JSONObject respObj = (JSONObject) JSONObject.parse(httpResponse.body());
                    String respStr = respObj.getString("data");
                    String xpath = "//html/body/div";
                    JXDocument jxDocument = new JXDocument(respStr);
                    List<Object> rs = jxDocument.sel(xpath);
                    for (Object o : rs) {
                        if (o instanceof Element) {
                            NewCrawlerInfo tempInfo = new NewCrawlerInfo();
                            //链接
                            String aStr = ((Element) o).outerHtml();
                            String apath = "//html/body/div/h3/a/@href";
                            JXDocument aJxDocument = new JXDocument(aStr);
                            List<Object> ars = aJxDocument.sel(apath);
//                        System.out.println(ars.get(0).toString());
                            tempInfo.setLink(ars.get(0).toString());
                            //标题
                            String titleStr = ((Element) o).outerHtml();
                            String titlepath = "//html/body/div/h3/a";
                            JXDocument titleJxDocument = new JXDocument(titleStr);
                            List<Object> titlers = titleJxDocument.sel(titlepath);
                            Object titleO = titlers.get(0);
                            String title = ((Element) titleO).html();
//                        System.out.println(title);
                            tempInfo.setTitle(title);
                            tempInfo.setId(title);
                            //其他信息 创建者，发布时间，目录 ，出处
                            String otherStr = ((Element) o).outerHtml();
                            String otherpath = "//html/body/div/span";
                            JXDocument otherJxDocument = new JXDocument(otherStr);
                            List<Object> otherrs = otherJxDocument.sel(otherpath);
                            if(otherrs.size()>=1){
                                Object otherO1 = otherrs.get(0);
                                tempInfo.setAuthor(((Element) otherO1).html());
                            }
                            if(otherrs.size()>=2) {
                                Object otherO2 = otherrs.get(1);
                                tempInfo.setTime(((Element) otherO2).html().replaceAll("发布时间:", ""));
                            }
                            if(otherrs.size()>=3) {
                                Object otherO3 = otherrs.get(2);
                                tempInfo.setMl(((Element) otherO3).html());
                            }
                            if(otherrs.size()>=4) {
                                Object otherO4 = otherrs.get(3);
                                tempInfo.setSource(((Element) otherO4).html());
                            }
                            crawlerInfos.add(tempInfo);
                        }
                    }
                    newCrawlerInfoService.saveOrUpdateBatch(crawlerInfos);
                }
            }
        } catch (Exception var7) {
            var7.printStackTrace();
        }

    }

    public static void main(String[] args) throws NoSuchFunctionException, XpathSyntaxErrorException, NoSuchAxisException {
        String keyword = "一眼千年 共绽芳华 文化遗产敦煌·彝绣携手走进东南大学";
        String queryStr = "[{\"field\":\"pageIndex\",\"value\":"+1+"},{\"field\":\"group\",\"value\":0},{\"field\":\"searchType\",\"value\":\"\"},{\"field\":\"keyword\",\"value\":\"" + keyword + "\"},{\"field\":\"recommend\",\"value\":\"1\"},{\"field\":4,\"value\":\"\"},{\"field\":5,\"value\":\"\"},{\"field\":6,\"value\":\"\"},{\"field\":7,\"value\":\"\"}]";
        String query = Base64Encoder.encode(queryStr.getBytes(StandardCharsets.UTF_8));
        List<NewCrawlerInfo> crawlerInfos = new ArrayList<>();
        Map<String, Object> fromMap = new HashMap<>();
        fromMap.put("searchInfo", query);
        HttpRequest post = HttpUtil.createPost("https://www.seu.edu.cn/_web/_search/api/searchCon/create.rst?_p=YXM9MiZ0PTI0OTImZD05NTk1JnA9MSZtPVNOJg__&tt=" + Math.random());
        post.header("Referer", "https://www.seu.edu.cn/_web/_search/api/search/new.rst?locale=zh_CN&request_locale=zh_CN&_p=YXM9MiZ0PTI0OTImZD05NTk1JnA9MSZtPVNOJg__");
        post.form(fromMap);
        HttpResponse httpResponse = post.execute();
        JSONObject respObj = (JSONObject) JSONObject.parse(httpResponse.body());
        String respStr = respObj.getString("data");
        String xpath = "//html/body/div";
        JXDocument jxDocument = new JXDocument(respStr);
        List<Object> rs = jxDocument.sel(xpath);
        for (Object o : rs) {
            if (o instanceof Element) {
                NewCrawlerInfo tempInfo = new NewCrawlerInfo();
                //链接
                String aStr = ((Element) o).outerHtml();
                String apath = "//html/body/div/h3/a/@href";
                JXDocument aJxDocument = new JXDocument(aStr);
                List<Object> ars = aJxDocument.sel(apath);
//                        System.out.println(ars.get(0).toString());
                tempInfo.setLink(ars.get(0).toString());
                //标题
                String titleStr = ((Element) o).outerHtml();
                String titlepath = "//html/body/div/h3/a";
                JXDocument titleJxDocument = new JXDocument(titleStr);
                List<Object> titlers = titleJxDocument.sel(titlepath);
                Object titleO = titlers.get(0);
                String title = ((Element) titleO).html();
//                        System.out.println(title);
                tempInfo.setTitle(title);
                tempInfo.setId(title);
                //其他信息 创建者，发布时间，目录 ，出处
                String otherStr = ((Element) o).outerHtml();
                String otherpath = "//html/body/div/span";
                JXDocument otherJxDocument = new JXDocument(otherStr);
                List<Object> otherrs = otherJxDocument.sel(otherpath);
                if(otherrs.size()>=1){
                    Object otherO1 = otherrs.get(0);
                    tempInfo.setAuthor(((Element) otherO1).html());
                }
                if(otherrs.size()>=2) {
                    Object otherO2 = otherrs.get(1);
                    tempInfo.setTime(((Element) otherO2).html().replaceAll("发布时间:", ""));
                }
                if(otherrs.size()>=3) {
                    Object otherO3 = otherrs.get(2);
                    tempInfo.setMl(((Element) otherO3).html());
                }
                if(otherrs.size()>=4) {
                    Object otherO4 = otherrs.get(3);
                    tempInfo.setSource(((Element) otherO4).html());
                }
                crawlerInfos.add(tempInfo);
            }
        }
    }

}
